package com.dmxh.controller;

import com.dmxh.aop.An;
import com.dmxh.params.QueryParams;
import com.dmxh.result.PageList;
import com.dmxh.result.Result;
import com.dmxh.service.TemplateService;
import com.dmxh.vo.TypeTemplateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/template")
@An
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @PostMapping("/queryPage")
    public Result<PageList<TypeTemplateVo>> queryPage(@RequestBody QueryParams<TypeTemplateVo>queryParams) {
        PageList<TypeTemplateVo> pageList = templateService.queryPage(queryParams);
        return new Result<>("1", "操作成功", pageList);
    }

    @GetMapping("/queryById/{id}")
    public Result queryById(@PathVariable("id")long id) {
        TypeTemplateVo typeTemplateVo = templateService.queryById(id);
        return new Result("1", "操作成功", typeTemplateVo);
    }

    @PostMapping("/save")
    public Result save(@RequestBody TypeTemplateVo typeTemplateVo) {
        //增加信息
        if (typeTemplateVo.getId() == null) {
            templateService.addInfo(typeTemplateVo);
        } else {
            //修改信息
            templateService.updateInfo(typeTemplateVo);
        }
        return Result.ok();
    }

    @DeleteMapping("/deleteInfo/{id}")
    public Result deleteInfo(@PathVariable("id")long id) {
        templateService.deleteInfo(id);
        return Result.ok();
    }

    @PatchMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody Integer[]ids) {
        templateService.deleteBatch(ids);
        return Result.ok();
    }

    @GetMapping("/queryAll")
    public Result queryAll() {
        return new Result("1", "操作成功", templateService.queryAll());
    }
}
